package cn.tedu.sp04.order.service;

import cn.tedu.sp01.pojo.Item;
import cn.tedu.sp01.pojo.Order;
import cn.tedu.sp01.pojo.User;
import cn.tedu.sp01.service.OrderService;
import cn.tedu.sp04.feign.ItemClient;
import cn.tedu.sp04.feign.UserClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
    @Autowired
    private UserClient userClient;
    @Autowired
    private ItemClient itemClient;

    /** 获取订单 */
    @Override
    public Order getOrder(String orderId) {
        //获取用户信息--真实项目是填写当前用户的id(cookie中存tooken,然后根据tooken从redis中获取user信息)
        User user = userClient.getUser(7).getData();
        //获取商品信息
        List<Item> items = itemClient.getItems(orderId).getData();
        //将查询的数据封装到order订单中进行封装回传
        Order order = new Order();
        order.setId(orderId);
        order.setUser(user);
        order.setItems(items);
        return order;
    }

    /** 新增订单 */
    @Override
    public void addOrder(Order order) {
        //增加用户的积分
        userClient.addScore(order.getUser().getId(),100);
        //将订单信息对应的商品从库存中减少
        itemClient.decreaseNumber(order.getItems());
        //将订单增加到数据库
        log.info("保存订单: "+order);
    }
}
